package com.hxzy.shop.controller.auth;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.cachedao.AuthCacheService;
import com.hxzy.shop.common.dto.auth.req.AccountListReq;
import com.hxzy.shop.common.dto.auth.req.AddAccountReq;
import com.hxzy.shop.common.dto.auth.req.EditAccountReq;
import com.hxzy.shop.common.dto.auth.resp.AccountDetailResp;
import com.hxzy.shop.common.dto.auth.resp.AccountListResp;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.dto.UserDto;
import com.hxzy.shop.dto.auth.req.LoadUserReq;
import com.hxzy.shop.dto.auth.resp.LoadUserResp;
import com.hxzy.shop.feign.auth.AuthFeign;
import com.hxzy.shop.utils.WebUtil;
import com.hxzy.shop.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述：商家账户信息
 *
 * @author tonneyyy
 */
@Api(tags = "总店后台-账户管理")
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    private AuthFeign authFeign;
    @Resource
    private AuthCacheService authCacheService;

    @ApiOperation(value = "获取当前用户信息", notes = "获取当前用户信息")
    @PostMapping(value = "/loadUser")
    public Result<LoadUserResp> loadUser() {
        String token = WebUtil.getRequest().getHeader(AuthConstant.JWT_TOKEN_HEADER);
        UserDto backUserInfo = authCacheService.getBackUserInfo(token);
        LoadUserReq req=new LoadUserReq();
        req.setAccountId(backUserInfo.getId());
        return authFeign.loadUser(req);
    }

    @ApiOperation(value = "账户查询")
    @GetMapping(value = "/accountList")
    public Result<PageInfo<AccountListResp>> accountList(@Valid AccountListReq req){
       return this.authFeign.accountList(req);
    }

    @ApiOperation(value = "新增账户")
    @PostMapping
    public Result<Object> addAccount(@RequestBody @Valid AddAccountReq req){
        return this.authFeign.addAccount(req);
    }

    @ApiOperation(value = "帐号详情", notes = "帐号详情")
    @GetMapping(value = "/{accountId}")
    public Result<AccountDetailResp> accountDetail(@PathVariable(value = "accountId") Long accountId) {
        return this.authFeign.accountDetail(accountId);
    }

    @ApiOperation(value = "修改账户")
    @PutMapping
    public Result<Object> editAccount(@RequestBody @Valid EditAccountReq req){
        return this.authFeign.editAccount(req);
    }
}
